package com.foreknow.web;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.foreknow.entity.Department;
import com.foreknow.entity.User;
import com.foreknow.service.impl.DepartmentServiceImpl;
import com.foreknow.utils.Constance;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/department")
public class DepartmentController {
    @Autowired DepartmentServiceImpl departmentService;

    //获取所有医生职称字段的方法
    @RequestMapping(value = "queryDeptName",method = RequestMethod.GET)
    public String queryDeptName(@RequestParam Map<String,Object> para) throws JsonProcessingException {
        HashMap<String,Object> hs =new HashMap();

        Integer pageNum =Integer.parseInt((String) para.get("pageNum"));
        Integer pageSize =Integer.parseInt((String)  para.get("pageSize"));

        //设置分页信息
        PageHelper.startPage(pageNum,pageSize);
        List list = departmentService.queryDeptName();


        //PageInfo是PageHelper中内置的分页信息类
        PageInfo<User> info = new PageInfo(list);
        //给map添加数据
        hs.put("total",info.getTotal());
        hs.put("pageNum",info.getPageNum());


       hs.put("list",list);
        if (list.size()>0){
            hs.put("meta",new Constance("获取用户所在科室成功",200));
        }else {
            hs.put("meta",new Constance("获取用户所在科室失败",401));
        }
        ObjectMapper objectMapper =new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    //根据id查询用户信息
    @RequestMapping(value = "selectUserById",method = RequestMethod.GET)
    public String selectUserById(@RequestParam Map<String,Object>para) throws JsonProcessingException {
        //获取表单信息
        HashMap<String,Object> hs = new HashMap<>();
        Integer id =Integer.parseInt((String) para.get("id"));
        Department department = departmentService.seletById(id);
        hs.put("department",department);
        if (department!=null){
            hs.put("meta",new Constance("根据id获取用户信息成功",200));
        }else {
            hs.put("meta",new Constance("根据id获取用户信息失败",401));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return  objectMapper.writeValueAsString(hs);
    }



    //根据条件查询信息
    @RequestMapping(value = "queryDep",method = RequestMethod.POST)
    @ResponseBody
    public String queryUser(@RequestBody Department department) throws JsonProcessingException{
        HashMap<String,Object> hs=new HashMap<>();
        //获取请求参数
        department.setDelmark(1);
        List<Department> depList = departmentService.selectByWhere(department);

        hs.put("department",depList);

        //添加meta
        if (depList.size()>0){
            hs.put("meta",new Constance("查询成功",200));
        }else {
            hs.put("meta",new Constance("查询失败",401));
        }
        ObjectMapper objectMapper =new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }


    //根据id删除用户信息
    @RequestMapping(value = "deleteDepById" ,method = RequestMethod.GET)
    public String deleteUserById(@RequestParam Map<String,Object>para) throws JsonProcessingException {
        HashMap<String,Object> hs=new HashMap<>();
        Integer id = Integer.parseInt((String)para.get("id"));
        int isRight = departmentService.deleteDepById(id);
        if (isRight==1){
            hs.put("meta",new Constance("删除成功",200));
        }else {
            hs.put("meta",new Constance("删除失败",401));
        }
        ObjectMapper objectMapper =new ObjectMapper();

        return objectMapper.writeValueAsString(hs);
    }

    //批量删除用户信息 的
    @RequestMapping(value = "deleteByChoose",method = RequestMethod.POST)
    public String deleteByChoose(@RequestBody Map<String ,Object>para) throws JsonProcessingException {
        HashMap<String,Object> hs=new HashMap<>();
        List<Integer> arr = new ArrayList<Integer>();
        arr= (List<Integer>) para.get("info");


        int sum =0;
        int isRight =0;
        for(Integer ss:arr
        ) {
            isRight = departmentService.deleteDepById(ss);
            sum = sum + isRight;
        }
        if (sum==arr.size()){
            hs.put("meta",new Constance("批量删除成功",200));
        }else {
            hs.put("meta",new Constance("批量删除失败",401));
        }
        ObjectMapper objectMapper =new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }


    //添加用户信息
    //添加用户信息
    @RequestMapping(value = "insertDep",method = RequestMethod.POST)
    @ResponseBody
    public String insertUser(@RequestBody Map<String,Object> para) throws JsonProcessingException {
        //获取表单信息
        HashMap<String,Object> hs = new HashMap<>();
        String deptCode = (String) para.get("deptcode");
        String deptName = (String) para.get("deptname");
        Integer deptCategoryID  = Integer.parseInt((String) para.get("deptcategoryid"));
//        Integer depID  = (Integer) para.get("Id");

        //传入Dep对象
        Department department =new Department();
        department.setDelmark(1);
//        department.setId(depID);
        department.setDeptcode(deptCode);
        department.setDeptname(deptName);
        department.setDeptcategoryid(deptCategoryID);
        int isRight = departmentService.insertDep(department);

        if(isRight==1){
            hs.put("meta",new Constance("添加用户成功",200));
        }else {
            hs.put("meta",new Constance("添加用户失败",401));
        }
        ObjectMapper objectMapper =new ObjectMapper();

        return objectMapper.writeValueAsString(hs);
    }

    //根据id修改用户信息
    @RequestMapping(value = "updateUser" ,method = RequestMethod.POST)
    public String updateUserById(@RequestBody Map<String,Object> para) throws JsonProcessingException {
        //获取表单信息
        //获取表单信息
        HashMap<String,Object> hs = new HashMap<>();
        String deptCode = (String) para.get("deptcode");
        String deptName = (String) para.get("deptname");
        Integer deptCategoryID  = (Integer) para.get("deptcategoryid");
        Integer depID  = (Integer) para.get("id");

        Department department =new Department();
        department.setDelmark(1);
        department.setId(depID);
        department.setDeptcode(deptCode);
        department.setDeptname(deptName);
        department.setDeptcategoryid(deptCategoryID);

        //调用service修改方法
        int isRight = departmentService.updateDep(department);
        if (isRight==1){
            hs.put("meta",new Constance("修改用户成功",200));
        }else {
            hs.put("meta",new Constance("修改用户失败",401));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return  objectMapper.writeValueAsString(hs);
    }

}
